<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div  class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <div>
          <a-row>
            <a-col style='height: 45px'>
              <a-form-model-item label='年级' prop='queryParam.gradeId'>
                <s-dict-select-tag :type='selectType' @change='changeGradeId' v-model='queryParam.gradeId'
                                   placeholder='请选择年级'
                                   dictCode='cou_grade,grade_name,id,tenant_id  is not null order by  grade_order' />
              </a-form-model-item>
            </a-col>
          </a-row>
        </div>
        <div v-if='toggleSearchStatus'>
          <a-row>
            <a-col style='height: 95px'>
              <a-form-model-item label='班级' prop='queryParam.classId'>
                <s-multi-select-tag :type='selectMultiType'
                                    v-model='queryParam.classId' placeholder='请选择班级'
                                    :is-pin-yin='isPinYin'
                                    :dictCode='queryParamSelect.classIdSelect' />
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col style='height: 75px'>
              <a-form-model-item label='科目' prop='queryParam.subjectId'>
                <s-dict-select-tag style='overflow-y: scroll;height: 70px' :type='selectType' placeholder='请选择科目' v-model='queryParam.subjectId'
                                   @change='handleSubjectChange'
                                   dictCode='cou_subject,subject_name,id,subject_iz_open =1 and del_flag =0  order by subject_order'></s-dict-select-tag>

              </a-form-model-item>
            </a-col>
          </a-row>

          <a-row>
            <a-col style='height: 120px'>
              <a-form-model-item label='教师'>
                <s-multi-select-tag :type='selectMultiType' :options='teacherList' v-model='queryParam.teacherId'
                                    placeholder='请选择教师'>
                </s-multi-select-tag>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col style='height: 45px'>
              <a-form-model-item label='建筑' prop='queryParam.buildingId'>
                <s-dict-select-tag class='a-row-under' style='overflow-y: scroll;height: 35px' :type='selectType' placeholder='请选择建筑' v-model='queryParam.buildingId'
                                   @change='handleBuildingChange'
                                   dictCode='fac_building,building_name,id,building_iz_open =1 order by building_classroom_capatity'></s-dict-select-tag>

              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col>
              <a-form-model-item label='教室'>
                <s-multi-select-tag :type='selectMultiType' :options='classroomList' v-model='queryParam.classroomId'
                                    :is-pin-yin='isPinYin'
                                    placeholder='请选择教室'>
                </s-multi-select-tag>
              </a-form-model-item>
            </a-col>
          </a-row>
        </div>
        <div>
          <a-row style='margin-top: 30px'>
            <a-col :offset='8'>
            <span>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
            </a-col>
          </a-row>
        </div>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <!--      <a-button type="primary" icon="download" @click="handleExportXls('rel_class_subject_teacher')">导出</a-button>-->
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"-->
      <!--                @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal"-->
      <!--                     @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
      <template>
        <span style="float:right;">
          <a @click="searchQuery()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <a-button v-print="'#datatable'" ghost type="primary">打印</a-button>
        </span>
      </template>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

<!--      <j-vxe-table-->
<!--        ref='vTable'-->
<!--        bordered-->
<!--        row-number-->
<!--        row-selection-->
<!--        rowKey='id'-->
<!--        class='j-table-force-nowrap'-->
<!--        keep-source-->
<!--        :columns='columns'-->
<!--        :dataSource='dataSource'-->
<!--        :pagination='ipagination'-->
<!--        :linkage-config='linkageConfig'-->
<!--        @pageChange='handlePageChange'-->
<!--        @edit-closed='handleEditClosed'-->
<!--      >-->
<!--      <span slot='action' slot-scope='text, record'>-->
<!--        <h1>asdfasdfasdf</h1>-->
<!--          <a @click='handleEdit(record)'>编辑</a>-->
<!--          <a-divider type='vertical' />-->
<!--          <a-dropdown>-->
<!--            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>-->
<!--            <a-menu slot='overlay'>-->
<!--              <a-menu-item>-->
<!--                <a @click='handleDetail(record)'>详情</a>-->
<!--              </a-menu-item>-->
<!--              <a-menu-item>-->
<!--                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>-->
<!--                  <a>删除</a>-->
<!--                </a-popconfirm>-->
<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
<!--        </span>-->
<!--      </j-vxe-table>-->
      <a-table
        id="datatable"
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class='j-table-force-nowrap'
        @change='handleTableChange'>
         <span slot='action' slot-scope='text, record'>
              <a @click='handleEdit(record)'>编辑</a>
              <a-divider type='vertical' />
              <a-dropdown>
                <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
                <a-menu slot='overlay'>
                  <a-menu-item>
                    <a @click='handleDetail(record)'>详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
      </a-table>
    </div>

    <rel-class-subject-teacher-modal ref='modalForm' @ok='modalFormOk'></rel-class-subject-teacher-modal>
  </a-card>
</template>

<script>

import moment from 'moment'
import { pushIfNotExist, randomNumber, randomUUID } from '@/utils/util'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import RelClassSubjectTeacherModal from './modules/RelClassSubjectTeacherModal'
import { getAction, putAction } from '@api/manage'
import { getBuildingDataList, getClassroomList, getSubjectList, queryTeacherListBySubject } from '@api/commonAPI'

export default {
  name: 'RelClassSubjectTeacherList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    RelClassSubjectTeacherModal
  },
  data() {
    return {
      description: 'rel_class_subject_teacher管理页面',
      linkageConfig: [
        // 可配置多个联动
        { requestData: this.loadMenu, key: 'subjectId_dictText' }
      ],
      // 表头
      columns: [
        {
          title: '班级',
          align: 'center',
          dataIndex: 'classId_dictText'
          // type: JVXETypes.input
        },
        {
          title: '科目',
          align: 'center',
          dataIndex: 'subjectId_dictText',
          //type: JVXETypes.select,
          // dictCode: 'cou_subject,subject_name,id'
          //linkageKey: 'teacherId_dictText'
        },
        {
          title: '任课教师',
          align: 'center',
          dataIndex: 'teacherId_dictText',
          //type: JVXETypes.select
        },
        // {
        //   title: '教室场地',
        //   align: 'center',
        //   dataIndex: 'classroomId_dictText'
        //   // type: JVXETypes.selectMultiple
        // },
        {
          title: '学年',
          align: 'center',
          dataIndex: 'schoolYearId_dictText'
          // type: JVXETypes.selectMultiple
        },
        {
          title: '学期',
          align: 'center',
          dataIndex: 'semesterId_dictText'
          // type: JVXETypes.selectMultiple
        },
        {
          title: '下拉框_搜索',
          dataIndex: 'classroomId',
          key: 'classroomId',
          type: JVXETypes.selectSearch,
          width: '180px',
          options: [
            {title: 'String', value: 'string'},
            {title: 'Integer', value: '1491674349357248584'},
            {title: '1491674349357248584', value: 'double'},
            {title: 'Boolean', value: 'boolean'}
          ],
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 160,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/rel_class_subject_teacher/relClassSubjectTeacher/list',
        edit: '/rel_class_subject_teacher/relClassSubjectTeacher/edit',
        delete: '/rel_class_subject_teacher/relClassSubjectTeacher/delete',
        deleteBatch: '/rel_class_subject_teacher/relClassSubjectTeacher/deleteBatch',
        exportXlsUrl: '/rel_class_subject_teacher/relClassSubjectTeacher/exportXls',
        importExcelUrl: 'rel_class_subject_teacher/relClassSubjectTeacher/importExcel',
        queryTeacherBySubject: '/rel_teacher_subject/relTeacherSubject/queryTeacherListBySubject',
        queryClassUrl: '/classes/couClass/queryByGradeId'
      },
      superFieldList: [],
      pagination: {},
      teacherList: [],
      classList: [],
      subjectList: [],
      buildingList: [],
      classroomList: [],
      selectType: 'radioButtonNew',
      selectTypeKuang: 'radioButtonNewKuang',
      selectMultiType: 'checkbox',
      isPinYin: false,
      //查询条件联动部分定义
      queryParamSelect: {}
    }
  },
  created() {
    this.getSuperFieldList()
    //this.initList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'subjectId', text: '科目id', dictCode: '' })
      fieldList.push({ type: 'string', value: 'teacherId', text: '任课教师id', dictCode: '' })
      fieldList.push({ type: 'string', value: 'classroomId', text: '教室场地id，允许空', dictCode: '' })
      this.superFieldList = fieldList
    },
    handlePageChange(event) {
      // 重新赋值
      this.pagination.current = event.current
      this.pagination.pageSize = event.pageSize
      // 查询数据
      this.loadData()
    },
    changeGradeId() {
      if (this.queryParam.gradeId) { //判断不为空
        this.queryParamSelect.classIdSelect = 'cou_class,class_name,id,class_iz_open  =1 and class_iz_delete  = 0 and grade_code=\'' + this.queryParam.gradeId + '\' order by  class_order'
      }
    },
    handleEditClosed(event) {
      let row = event.row
      console.log(row)
      let params = {
        id: row.id,
        subjectId: row.subjectId_dictText,
        teacherId: row.teacherId_dictText
      }
      // console.log(params)
      putAction(this.url.edit, params).then(res => {
        if (res.success) {
          this.$message.success('保存成功')
        } else {
          this.$message.warning('保存失败')
        }
      })
    },
    async loadMenu(parent) {
      let res
      let list = []
      if (parent === '') {
        res = await getAction('/rel_teacher_subject/relTeacherSubject/querySubject')
        res.result.forEach(item => {
          list.push({
            text: item.subjectName,
            value: item.subjectId
          })
        })
        return list
      } else {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        let tenantId = localData.value.tenantId
        let param = {
          subjectId: parent,
          tenantId: tenantId
        }
        res = await getAction('/rel_teacher_subject/relTeacherSubject/queryTeacherListBySubject', param)
        res.result.forEach(item => {
          list.push({
            text: item.teacherName,
            value: item.teacherId
          })
        })
        return list
      }
      this.$message.error('级联失败,查找数据为空')
      return list
    },
    async handleSubjectChange() {
      let tenantId = ''
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        tenantId = localData.value.tenantId
        // this.model.tenantId = tenantId
      }
      let params = {
        tenantId: tenantId,
        subjectId: this.queryParam.subjectId
      }
      console.log(params)
      if (this.queryParam.subjectId !== null) {
        let res = await queryTeacherListBySubject(params)
        if (res.success) {
          this.teacherList = []
          res.result.forEach(item => {
            this.teacherList.push({
              value: item.teacherId,
              label: item.teacherName
            })
          })
        }
      }
    },
    async initList() {
      let subjectList = await getSubjectList()
      if (subjectList.code === 200) {
        this.subjectList = []
        subjectList.result.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.subjectId
          })
        })
      }
      // let buildingList = await getBuildingDataList()
      // if (buildingList.code === 200) {
      //   this.buildingList = []
      //   buildingList.result.forEach(item => {
      //     this.buildingList.push({
      //       label: item.buildingName,
      //       value: item.id
      //     })
      //   })
      //
      // }
    },
    async handleBuildingChange() {
      let params = {
        buildingId: this.queryParam.buildingId
      }
      let classroomList = await getClassroomList(params)
      if (classroomList.code === 200) {
        this.classroomList = []
        classroomList.result.forEach(item => {
          this.classroomList.push({
            label: item.classroomName,
            value: item.id
          })
        })

      }
    }

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>